package com.ea.modular.controller.sys;



import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ea.common.annotation.LogAnnotation;
import com.ea.common.node.ZTreeNode;
import com.ea.common.parent.controller.BaseController;
import com.ea.common.utils.ToolUtil;
import com.ea.modular.model.sys.Menu;
import com.ea.modular.service.sys.MenuService;
import com.ea.modular.utils.sys.UserUtil;

import io.swagger.annotations.ApiOperation;

/**
 * 菜单实现控制器
 * @author jun
 *
 */
@Controller
@RequestMapping("/menu")
public class MenuController extends BaseController {

	private String PREFIX = "/system/menu/";

	@Autowired
	private MenuService menuService;
	/**
	 * 跳转到菜单管理首页
	 */
	@RequestMapping("")
	@RequiresPermissions("sys:menu:view")
	public String index() {
		return PREFIX + "menu";
	}


	/**
	 * 获取所有部门列表
	 */
	@LogAnnotation
	@ApiOperation(value = "菜单列表")
	@RequestMapping(value = "/list")
	@ResponseBody
	public Object list(String name) {
		List<Menu> list = UserUtil.getMenus(name);
		return list;

	}


	/**
	 * 删除菜单信息
	 */
	@LogAnnotation
	@ApiOperation(value = "删除菜单")
	@RequestMapping(value = "/delete")
	@ResponseBody
	public Object delete(String id) {
		//删除菜单关联的缓存
		UserUtil.clearMenuCache();
		return menuService.delete(id);
	}
	
	
	/**
	 * 菜单from页确认返回是添加还是修改
	 * @return
	 */
	@ApiOperation(value = "菜单详情")
	@RequestMapping(value = "/form")
	public String form(Model model,String id,String type) {
		
		if(type.equals("update")) {
			Menu menu = UserUtil.getMenuById(id);;
	    	Menu pm = UserUtil.getMenuById(menu.getParentId());
	    	model.addAttribute("pName",pm!=null?pm.getName():"顶级");
	    	model.addAttribute(menu);
			return PREFIX + "menu_edit";
		}
		return PREFIX + "menu_add";
	}

	
	/**
	 * 菜单保存
	 * @return
	 */
	@LogAnnotation
	@ApiOperation(value = "菜单保存")
	@RequestMapping(value = "/save",method=RequestMethod.POST)
	@ResponseBody
	public int save(Menu menu,String type) {
		//完善PIDS
		menuSetPids(menu);
		//删除菜单关联的缓存
		UserUtil.clearMenuCache();
		//判断是否为修改
		if(type.equals("update")) {
			
			return menuService.update(menu);
		}
		return menuService.insert(menu);
	}
	
	  
    /**
     * 获取菜单的tree列表
     */
    @RequestMapping(value = "/tree")
    @ResponseBody
    public List<ZTreeNode> tree() {
        List<ZTreeNode> tree = UserUtil.getMenutree();
//        tree.add(ZTreeNode.createParent());
        return tree;
    }
    
    
  
    
    /**
     * 根据角色ID获取授权的菜单
     */
    @RequestMapping(value = "/menuTreeListByRoleId/{roleId}")
    @ResponseBody
    public List<ZTreeNode> menuTreeListByRoleId(@PathVariable String roleId) {
    	List<String> menuIds = menuService.getRoleIdByMenuIds(roleId);
        if (ToolUtil.isNotEmpty(menuIds)) {
            List<ZTreeNode> roleTreeList = this.menuService.menuTreeListByMenuIds(menuIds);
            return roleTreeList;
        } else {
            List<ZTreeNode> roleTreeListByUserId = UserUtil.getMenutree();
            return roleTreeListByUserId;
        }
    }
    
    
    /**
     * 完善PIDS
     * @param Menu
     */
    private void menuSetPids(Menu menu) {
        if (ToolUtil.isEmpty(menu.getParentId()) || menu.getParentId().equals("0")) {
        	menu.setParentId("0");
        	menu.setParentIds("0,");
        } else {
            String pid = menu.getParentId();
            Menu m = UserUtil.getMenuById(pid);
            String pids = m.getParentIds();
            menu.setParentId(pid);
            menu.setParentIds(pids+pid+",");
        }
    }
    
    /**
     * 跳转到图标页面
     */
    @RequestMapping("/fontawesome")
    public String fontawesome(ModelMap modelMap,String id) {
        modelMap.put("id",id);
        return "/common/_fontawesome";
    }
    
}
